package com.gao.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Component
public class UserSchema {
    /**
     * 日志实现
     */
    private final Logger logger = LoggerFactory.getLogger ( UserSchema.class );


    /**
     * 前置通知
     *
     * @param jp
     */
    public void before(JoinPoint jp) {
        logger.info ( "前置通知========》》》》" );
        logger.info ( "目标对象" + jp.getTarget () + "  方法名" + jp.getSignature ().getName () + "  参数" + Arrays.toString ( jp.getArgs () ) );
    }

    /**
     * 后置通知
     * 最终通知,是否出现异常都会被执行
     *
     * @param jp
     */
    public void afert(JoinPoint jp) {
        logger.info ( "后置通知 After========》》》》" );
    }

    /**
     * 后置通知，returning可以接受到ProductDao中方法的返回值
     *
     * @param resous
     */
    public void afterReturning(Object resous) {
        logger.info ( "后置通知 AfterReturning ========》》》》" + resous );
    }

    /**
     * 异常时  后置通知
     *
     * @param e
     */
    public void afterThrowing(JoinPoint jp,RuntimeException e) {
        logger.info ( "AfterThrowing后置通知    异常是:" + e );
    }

    /**
     * 环绕通知
     * @param pj
     */
    public void around(ProceedingJoinPoint pj) {
        Object proceed = null;
        try {
            logger.info ( "around前置通知===" );
             proceed = pj.proceed ();
            logger.info ( "around后置通知" );

        } catch (Throwable throwable) {
            throwable.printStackTrace ();
        }
    }
}
